﻿using System;
using System.Threading;
using Cysharp.Threading.Tasks;
using IQIGame.Onigao.Framework;
using UnityEngine;

namespace IQIGame.Onigao.GamePlay
{
    public class StoryAction_Animation : StoryAction
    {
        private string _animation;
        private bool _isLoop;

        protected override bool OnInitialize(GameObject root, StoryActionData data)
        {
            base.OnInitialize(root, data);
            if (data is StoryActionData_Animation rAnimation)
            {
                this._animation = rAnimation.animation;
                this._isLoop = rAnimation.loop;
                return true;
            }
            return false;
        }

        protected override async UniTask OnStart(CancellationToken cancel)
        {
            await base.OnStart(cancel);
            var rPlayer = this.GetDynamic<Action<string, bool>>(EStoryActionDynamicKey.Animation);
            rPlayer?.Invoke(this._animation, this._isLoop);
        }

        public override void OnReset()
        {
            base.OnReset();
            this._animation = default;
            this._isLoop = default;
        }

        public override void Free()
        {
            ClassPool.Put(this);
        }
    }
}